package com.gc.ip;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class JMultiIPAddress extends JPanel
{
	private JIPAddress ipAddress = null;
	private JButton reportButton = null;
	
	private JIPAddressTable ipAddressTable = null;
	
	public JMultiIPAddress()
	{
		this(null, null);
	}
	
	public JMultiIPAddress(JIPAddress ipAddress)
	{
		this(ipAddress, null);
	}
	
	public JMultiIPAddress(JIPAddress ipAddress, Vector<IPAddress> tableData)
	{
		this.ipAddress = ipAddress;
		if(this.ipAddress == null)
		{
			this.ipAddress = new JIPAddress();
		}
		ipAddressTable = new JIPAddressTable(this.ipAddress, tableData);
		initGUI();
		addListeners();
	}
	private void initGUI()
	{
		reportButton = new JButton("IP Address Report");
		
		JPanel editorPanel = new JPanel();
		editorPanel.setLayout(new GridBagLayout());
		editorPanel.add(getLabel(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		editorPanel.add(ipAddress, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		editorPanel.add(reportButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		
		this.setLayout(new BorderLayout());
		this.add(editorPanel, BorderLayout.NORTH);
		this.add(ipAddressTable, BorderLayout.CENTER);
		
//		this.setBorder(new TitledBorder("IP Address"));
	}
	private JLabel getLabel()
	{
		return new JLabel("IP Address/Prefix Length");
	}
	private void addListeners()
	{
		
	}
	
	public JIPAddressTable getJIPAddressTable()
	{
		return ipAddressTable;
	}
	
	public IPAddress getIPAddress(int row)
	{
		return ipAddressTable.getIPAddress(row);
	}
	
	public void setIPAddress(int row, IPAddress ipAddress)
	{
		//TODO
	}
	
	public void removeIPAddress(int row)
	{
		//TODO
	}
	
	public void removeAllIPAddress()
	{
		//TODO
	}
	
	public List<IPAddress> getAllIPAddress()
	{
		return ipAddressTable.getAllIPAddress();
	}
	
	@Override
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		ipAddressTable.setEnabled(enabled);
		ipAddress.setEnabled(enabled);
		reportButton.setEnabled(enabled);
	}
//	javax.swing.UIManager.put("swing.boldMetal", Boolean.FALSE);
//	javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
	
	public static void main(String[] args) throws Exception
	{
		JFrame frame = new JFrame("JMultiIPAddress Test");

		//Create a JIPAddress instance. It is actually a JPanel
		//The component has PrefixLength and allows both IPv4 and IPv6
		JIPAddress jIpAddress = new JIPAddress(true, IPType.IPv4_v6);
		
		//The initial data
		Vector<IPAddress> initDataVec = new Vector<IPAddress>();
		initDataVec.add(new IPAddress("192.168.0.1", 30));
		initDataVec.add(new IPAddress("2100::", 124));
		initDataVec.add(new IPAddress("192.168.0.1", "255.255.0.0"));
		initDataVec.add(new IPAddress("2100::", "FFFC::"));
		
		//Create a JMultiIPAddress component with some existing data. 
		//It is actually a JPanel, so you can easily add it to a Swing Container.
		JMultiIPAddress multiIPAddress = new JMultiIPAddress(jIpAddress, initDataVec);

		//Add the component to the frame
		frame.getContentPane().add(multiIPAddress);

		//GUI related operations
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(700, 200));
		frame.pack();
		frame.setVisible(true);
	}
}
